package org.bes;


import org.bes.api.IPatchFactory;
import org.bes.api.*;
import org.bes.WinClass.PatchFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

//替换class


public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {

        if (args.length < 1) {
            System.out.println("Usage: java PatchTool <command> <args>");
            System.out.println("Commands:");
            System.out.println("  patch -update <patch_file> <patch_file> ...");
            System.out.println("  patch -rollback");
            System.out.println("  patch -delete");
            return;
        }

        System.out.println("Press any key to continue...");
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();

        String command = args[0];
        System.out.println(Arrays.toString(args));

        IPatchFactory patchFactory = new PatchFactory();
        IRollbacker rollbacker = patchFactory.createRollbacker();
        IDelete delete = patchFactory.createDelete();


        System.out.println("Command: " + command);
        switch (command) {
            case "-rollback":
                if (args.length != 1) {
                    System.out.println("Usage: java PatchTool -rollback");
                    return;
                }

                rollbacker.rollback();
                delete.delete();
                System.out.println();
                // Handle the -rollback command and perform rollback
                break;

            case "-update":
                if (args.length < 3) {
                    System.out.println("Usage: java PatchTool -update <patch_file>");
                    return;
                }

                Path patchFile = Paths.get(System.getProperty("user.dir")).resolve(Paths.get(args[2]));
                System.out.println("Processing patch file: " + patchFile);

                try {
                    // Create an extractor
                    IExtractor extractor = patchFactory.createExtractor();
                    extractor.extract();
                    System.out.println();

                    // Backup files
                    IBackup backup = patchFactory.createBackup();
                    backup.backup();
                    System.out.println();

                    // Apply the patch
                    IUpdate update = patchFactory.createUpdate();
                    int status = update.update();

                    if (status == 2) {
                        System.out.println("Update failed for " + patchFile + ". Rolling back...");

                        rollbacker.rollback();
                        delete.delete();
                    }
                    System.out.println();
                } catch (Exception e) {
                    System.out.println("Error processing patch file: " + patchFile);
                    e.printStackTrace();
                }

                System.out.println();
                break;

            case "-delete":
                if (args.length != 1) {
                    System.out.println("Usage: java PatchTool -delete");
                    return;
                }

                delete.delete();
                // Handle the -delete command and perform deletion
                break;

            default:
                System.out.println("Unknown command: " + command);
                break;
        }
    }
}
